package com.house.reportisy;

import com.house.entity.User;
import com.house.vo.UserVO;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * @version 1.0
 * @Author OhygC
 * @since 1.0
 */
@EnableJpaRepositories
@Repository
public interface UserRepository extends JpaRepository<User, Integer>, JpaSpecificationExecutor<User> {
    /**
     * 根据电话查询用户
     */
    User getUserByPhone(String phone);


    @Transactional
    @Modifying
    @Query(value = "update user set username=?,pass_word=? where id=?", nativeQuery = true)
    int updateUsername(String username, String password, Integer id);

    /**
     * 根据手机号查询盐值
     */
    User getSaltByPhone(String phone);

    @Query(value = "select salt from user where id=?", nativeQuery = true)
    String getSaltById(Integer id);

    @Query(value = "select id,username,u_name as uname,phone,idcard,user_role as userRole,state from user where del_flag=0 and user_role='普通用户'", nativeQuery = true)
    Page<Map<String, Object>> nativeQueryPagingList(Pageable pageable);

    @Query(value = "select id,username,u_name as uname,phone,idcard,user_role as userRole,state from user where del_flag=0 and user_role='房东'", nativeQuery = true)
    Page<Map<String, Object>> nativeQueryPagingList1(Pageable pageable);

    /**
     * 根据软删除和角色查询所有用户数量
     */
    int countUserByDelFlagAndUserRoleEquals(Integer number, String role);

    /**
     * 查询状态码的所有用户数量
     */
    int countUserByStateEquals(Integer state);

    /**
     * 修改状态码为1，标识已提交审核
     */
    @Transactional
    @Modifying
    @Query(value = "update user set state=1 where id=?", nativeQuery = true)
    int updateStateById(Integer id);

    /**
     * 修改状态码为2，标识通过审核
     */
    @Transactional
    @Modifying
    @Query(value = "update user set user_role='房东' , state=2 where id=?", nativeQuery = true)
    int updateState1ById(Integer id);

    /**
     * 查询状态码为1的所有用户，标识待审核用户
     */
    @Query(value = "select id,username,u_name as uname,phone,idcard,user_role as userRole,state from user where del_flag=0 and state=1", nativeQuery = true)
    Page<Map<String, Object>> nativeQueryPagingList2(Pageable pageable);

    //@Query(value = "select id,username,u_name,phone,idcard,user_role,state from user where del_flag=0 and name=?", nativeQuery = true)
    //List<Map<String, UserVO>> nativeQueryPagingList3(Class<UserVO> userVOClass, Pageable pageable, String uname);

    //Page<User> findAll(Specification<UserVO> specification, Pageable pageable);
}
